package com.shanzmoo.db.module.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.db.module.member.domain.query.MemberActivityPageQuery;
import com.shanzmoo.db.module.member.domain.vo.MemberActivityConfigVO;
import com.shanzmoo.db.module.member.entity.MemberActivityConfigEntity;
import com.shanzmoo.db.module.member.entity.MemberMerchantEntity;
import com.shanzmoo.db.module.member.mapper.MemberActivityConfigMapper;
import com.shanzmoo.db.module.member.service.IMemberActivityConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 会员活动 服务实现类
 * </p>
 *
 * @author wangyudi
 * @since 2021-03-29
 */
@Service
public class MemberActivityConfigServiceImpl extends ServiceImpl<MemberActivityConfigMapper, MemberActivityConfigEntity> implements IMemberActivityConfigService {

    @Override
    public Page<MemberActivityConfigEntity> getByPage(MemberActivityPageQuery request) {
        String activityId = request.getActivityId();
        String activityName = request.getActivityName();
        Integer activityObj = request.getActivityObj();
        Integer activityType = request.getActivityType();
        Integer status = request.getStatus();
        String createTimeStart = request.getCreateTimeStart();
        String createTimeEnd = request.getCreateTimeEnd();
        String startTimeStart = request.getStartTimeStart();
        String startTimeEnd = request.getStartTimeEnd();
        String endTimeStart = request.getEndTimeStart();
        String endTimeEnd = request.getEndTimeEnd();
        String merchantCode = request.getMerchantCode();
        return page(
                request.page(),
                new LambdaQueryWrapper<MemberActivityConfigEntity>()
                .eq(!StringUtils.isEmpty(activityId),MemberActivityConfigEntity::getId,activityId)
                .eq(!StringUtils.isEmpty(activityName),MemberActivityConfigEntity::getName,activityName)
                .eq(activityObj!=null,MemberActivityConfigEntity::getActivityObj,activityObj)
                .eq(activityType!=null,MemberActivityConfigEntity::getActivityType,activityType)
                .eq(status!=null,MemberActivityConfigEntity::getStatus,status)
                .between(!StringUtils.isEmpty(createTimeStart) && !StringUtils.isEmpty(createTimeEnd),MemberActivityConfigEntity::getCreateTime,createTimeStart,createTimeEnd)
                .between(!StringUtils.isEmpty(startTimeStart) && !StringUtils.isEmpty(startTimeEnd),MemberActivityConfigEntity::getStartTime,startTimeStart,startTimeEnd)
                .between(!StringUtils.isEmpty(endTimeStart) && !StringUtils.isEmpty(endTimeEnd),MemberActivityConfigEntity::getEndTime,endTimeStart,endTimeStart)
                .eq(MemberActivityConfigEntity::getDeleted,false)
                .eq(!StringUtils.isEmpty(merchantCode),MemberActivityConfigEntity::getMerchantCode,merchantCode)
        );
    }

    @Override
    public Page<MemberActivityConfigVO> queryPlfByRequest(MemberActivityPageQuery request) {
        return baseMapper.queryPlfByRequest(request.page(),request);
    }

    /**
     * 获取商户所有活动
     * @param merchantCode
     * @return
     */
    @Override
    public List<MemberActivityConfigEntity> getbyMerchantCode(String merchantCode) {
        return list(
                new LambdaQueryWrapper<MemberActivityConfigEntity>()
                .eq(MemberActivityConfigEntity::getMerchantCode,merchantCode)
                .eq(MemberActivityConfigEntity::getDeleted,false)
        );
    }
}
